/*
 * @Author: song
 * @Description:当前刻度
 * @Date: 2024年04月28日 星期日 20:04:19
 * @Modify:
 */

using SCore.Editor.WindowDraw;
using SCore.Function.Fixed;
using UnityEditor;
using UnityEngine;

namespace Function.SkillEditor.Editor
{
    public class SkillTimeLineCursorView : NCommonView
    {
        private SkillTimeLineMarkerView _markerView;
        private SkillTimeLineHeadView _headView;
        private fp curFrame;

        public fp _curFrame
        {
            get { return curFrame; }
            set
            {
                if (!_headView._play && _curFrame.AsInt() != value.AsInt())
                {
                    SkillEditorWindow.EventManager.BroadCast(SkillEditorEventType.TimeLinePlayFrameChange,
                        value);
                }

                curFrame = value;
            }
        }

        private Rect _timeCursorRect;

        public void Bind(SkillTimeLineMarkerView markerView, SkillTimeLineHeadView headView)
        {
            _markerView = markerView;
            _headView = headView;
        }

        public void OnCursorGUI()
        {
            if (Event.current.type == EventType.Repaint)
            {
                var parent = GetSubEditorWindow<SkillTimeLineSubWindow>();
                var position = parent.GetPosition();
                float x = _markerView.GetXByFrame(_curFrame.AsInt());

                UpdateArea(new Rect(x - 5, 2, 20, 40));
                EditorDrawHelp._timeCursor.Draw(_rect, new GUIContent(""), false, false,
                    false, false);

                EditorGUI.DrawRect(new Rect(_rect.x + 5, _rect.y, 2, position.height),
                    EditorDrawHelp._white);

                GUI.Label(new Rect(_rect.x + 12, _rect.y, 20, 20),
                    _curFrame.AsInt().ToString(),
                    EditorDrawHelp.GetTextGUIStyle(15, TextAnchor.UpperLeft, EditorDrawHelp._crimsonRed));
            }
        }

        protected override Vector2 GetMousePos(Event evt)
        {
            return evt.mousePosition - new Vector2(_markerView.GetRect().x, _markerView.GetRect().y);
        }

        protected override bool MouseDrag(Event evt)
        {
            if (evt.button == 0)
            {
                var dragFrame = _markerView.GetIntFrameByX(GetMousePos(evt).x);
                if (dragFrame >= 0)
                {
                    _curFrame = dragFrame;
                }

                return true;
            }

            return false;
        }
    }
}